package Interface;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import controleur.ControleurCommis;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Observable;
import java.util.Observer;

@SuppressWarnings("serial")
public class Authentification extends JFrame implements Observer {

	private JTextField txtMotDePasse;
	private JTextField txtNomUsager;
	public Reservation interfaceReservation;
	private ControleurCommis controleurCommis;

	/**
	 * Create the application.
	 */
	public Authentification(Reservation pInterfaceReservation ) {
		setTitle("Connexion");
		initialize();
		interfaceReservation = pInterfaceReservation;
		controleurCommis = new ControleurCommis();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		this.setResizable(false);
		this.setBounds(100, 100, 280, 143);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.getContentPane().setLayout(null);
		
		JLabel lblNomUsager = new JLabel("Nom d'usager:");
		lblNomUsager.setBounds(10, 11, 84, 14);
		this.getContentPane().add(lblNomUsager);
		
		JLabel lblMotDePasse = new JLabel("Mot de passe:");
		lblMotDePasse.setBounds(10, 36, 84, 14);
		this.getContentPane().add(lblMotDePasse);
		
		JButton btnConnexion = new JButton("Connexion");
		btnConnexion.addActionListener(new AuthentificationListener(this));
		btnConnexion.setBounds(169, 81, 95, 23);
		this.getContentPane().add(btnConnexion);
		
		txtMotDePasse = new JPasswordField();
		txtMotDePasse.setBounds(114, 33, 150, 20);
		this.getContentPane().add(txtMotDePasse);
		txtMotDePasse.setColumns(10);
		
		txtNomUsager = new JTextField();
		txtNomUsager.setBounds(114, 8, 150, 20);
		this.getContentPane().add(txtNomUsager);
		txtNomUsager.setColumns(10);
	}
	
	class AuthentificationListener implements ActionListener {
		public Observer sender;
		public AuthentificationListener(Observer pSender){
			sender = pSender;
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			
			if (txtNomUsager.getText().trim().length() <= 0 ||
				txtMotDePasse.getText().trim().length() <= 0)
			{
				javax.swing.JOptionPane.showMessageDialog(null, "Le nom d'usager ou le mot de passe n'est pas valide.");
				return;				
			}
			
			controleurCommis.Authentification(txtNomUsager.getText(), txtMotDePasse.getText(),sender);
		}
	}

	@Override
	public void update(Observable o, Object arg) {
		interfaceReservation.setVisible(true);
		this.dispose();
	}
}
